export default {
  namespaced: true,
  
  state: () => ({
    address: JSON.parse(uni.getStorageSync('address') || '{}'),
    // 用户登录成功后会有个 token 值
    token: uni.getStorageSync('token') || '',
    // 用户登录后的信息
    userinfo: JSON.parse(uni.getStorageSync('userinfo') || '{}'),
    // 登录后需要重定向的可以设置这个值 {openType: '什么样的方式跳转，跳转tabbar或普通页面', url:'跳转的路径'}
    redirectInfo: null
  }),
  
  mutations: {
    updateAddress(state, address) {
      state.address = address
      
      this.commit('m_user/saveAddressToStorage')
    },
    saveAddressToStorage(state) {
      uni.setStorageSync('address', JSON.stringify(state.address))
    },
    updateUserinfo(state, userinfo) {
      state.userinfo = userinfo
      
      this.commit('m_user/saveUserinfoToStorage')
    },
    saveUserinfoToStorage(state) {
      uni.setStorageSync('userinfo', JSON.stringify(state.userinfo))
    },
    updateToken(state, token) {
      state.token = token
      
      this.commit('m_user/saveTokenToStorage')
    },
    saveTokenToStorage(state) {
      uni.setStorageSync('token', JSON.stringify(state.token))
    },
    userLoginOut(state) {
      // 退出登录
      state.address = {}
      state.userinfo = {}
      state.token = ''
      
      // 移除本地存储 键
      uni.removeStorageSync('address')
      uni.removeStorageSync('token')
      uni.removeStorageSync('userinfo')
    },
    updateRedirectInfo(state, info) {
      state.redirectInfo = info
    }
  },
  
  getters: {
    detailsAddress(state) {
      // 如果为null，则返回 '' 否则拼接地址
      if(JSON.stringify(state.address) === '{}') return ''
      return state.address.provinceName + state.address.cityName + state.address.countyName + state.address.detailInfo
    }
  }
}
